package lookas.foto;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lookas.track.GPSPosition;

import org.cmc.sanselan.ImageReadException;
import org.cmc.sanselan.ImageWriteException;
import org.cmc.sanselan.Sanselan;
import org.cmc.sanselan.formats.jpeg.JpegImageMetadata;
import org.cmc.sanselan.formats.jpeg.exifRewrite.ExifRewriter;
import org.cmc.sanselan.formats.tiff.TiffField;
import org.cmc.sanselan.formats.tiff.constants.TagInfo;
import org.cmc.sanselan.formats.tiff.fieldtypes.FieldType;
import org.cmc.sanselan.formats.tiff.write.TiffOutputSet;

public class FotoPosition extends GPSPosition{
    private File foto;

    public FotoPosition(File foto) throws ImageReadException, IOException{
        this.foto = foto;
        this.date = FotoPosition.readDate(foto);
    }

    public void setDeltaTime(int timedelta){
        date.add(Calendar.MILLISECOND, timedelta);
    }

    @Override
    public String toString(){
        return foto.getName() + ": " + super.toString();
    }

    public File getFoto(){
        return foto;
    }

    public static Calendar readDate(File foto) throws ImageReadException,
        IOException{

        JpegImageMetadata metadata = (JpegImageMetadata)Sanselan
            .getMetadata(foto);
        TiffField tf = metadata.findEXIFValue(new TagInfo("Date Time Original",
            36867, FieldType.FIELD_TYPE_ASCII));
        String value = (String)tf.getValue();
        Date date;
        try{
            date = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").parse(value);
        }catch(ParseException e){
            throw new ImageReadException(foto.getAbsolutePath(), e);
        }
        Calendar ret = Calendar.getInstance();
        ret.setTime(date);
        return ret;
    }

    public void writeGPSPosition() throws ImageReadException, IOException,
        ImageWriteException{

        JpegImageMetadata metadata = (JpegImageMetadata)Sanselan
            .getMetadata(foto);
        TiffField tf = metadata.findEXIFValue(new TagInfo("Date Time Original",
            36867, FieldType.FIELD_TYPE_ASCII));
        String s = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(date
            .getTime());
        byte[] bytes = (s + (char)0).getBytes();
        tf.setOversizeValue(bytes);
        TiffOutputSet outputSet = metadata.getExif().getOutputSet();
        outputSet.setGPSInDegrees(position.getLon(), position.getLat());
        File f = new File(foto.getParentFile(), "gps");
        f.mkdirs();
        File file = new File(f, foto.getName());
        file.createNewFile();
        OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
        ExifRewriter exifRewriter = new ExifRewriter();
        exifRewriter.updateExifMetadataLossless(foto, os, outputSet);
        foto = file;
        os.close();
    }
}
